package com.business_clean.ui.activity.custom;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.config.ConstantMMVK;
import com.business_clean.app.uitl.MMKVHelper;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.app.uitl.UploadFileHelper;
import com.business_clean.data.mode.init.BaseIDNameEntity;
import com.business_clean.data.mode.init.InitDataEntity;
import com.business_clean.data.mode.project.StringProjectEntity;
import com.business_clean.data.mode.project.WorkRulesEntity;
import com.business_clean.databinding.ActivityAddProjectBinding;
import com.business_clean.ui.adapter.BaseStringAdapter;
import com.business_clean.ui.adapter.personnel.AddProjectAdapter;
import com.business_clean.ui.adapter.viewpager2.FragmentLazyStateAdapter;
import com.business_clean.ui.fragment.addproject.BankCardFragment;
import com.business_clean.ui.fragment.addproject.ContactInformationFragment;
import com.business_clean.ui.fragment.addproject.EmploymentInformationFragment;
import com.business_clean.ui.fragment.addproject.IdCardFragment;
import com.business_clean.ui.fragment.addproject.OtherFragment;
import com.business_clean.ui.fragment.addproject.SalaryFragment;
import com.business_clean.viewmodel.request.ProjectMembersViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.decoration.MyGridSpacingItemDecoration;
import com.luck.picture.lib.entity.LocalMedia;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import me.hgj.mvvmhelper.base.BaseViewModel;
import me.hgj.mvvmhelper.ext.AppExtKt;


public class AddProjectActivity extends BaseActivity<ProjectMembersViewModel, ActivityAddProjectBinding> {

    private AddProjectAdapter mAdapter;


    public static String ID_CARD = "ID_CARD";
    public static String ID_BANK = "ID_BANK";
    public static String ID_WORK_INFO = "ID_WORK_INFO";
    public static String ID_PIC = "ID_PIC";
    public static String ID_SALARY = "ID_SALARY";
    public static String ID_CONTACTS = "ID_CONTACTS";

    public static String ID_SIGN_HAND = "ID_SIGN_HAND";

    public static InitDataEntity initDataEntity;
    List<Fragment> fragmentList = new ArrayList<>();

    private int type = 0;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        if (getIntent() != null && getIntent().getExtras() != null) {
            mDatabind.tabbar.setTitle("编辑项目成员");
            type = getIntent().getExtras().getInt("type", 0);
        }

        mViewModel.requestWorkRules();

        String initDataJson = MMKVHelper.getString(ConstantMMVK.INIT_DATA);
        initDataEntity = new Gson().fromJson(initDataJson, InitDataEntity.class);

        mAdapter = new AddProjectAdapter(1);
        mDatabind.recyclerview.setAdapter(mAdapter);
        mDatabind.recyclerview.setLayoutManager(new GridLayoutManager(this, 4));
        mDatabind.recyclerview.addItemDecoration(new MyGridSpacingItemDecoration(4, SizeUtils.dp2px(8), true));

        List<StringProjectEntity> strings = new ArrayList<>();

        boolean needBan = false;
        if (type != 3) {
            needBan = true;
        }

        strings.add(new StringProjectEntity("身份信息", false));
        strings.add(new StringProjectEntity("联系信息", needBan));
        strings.add(new StringProjectEntity("银行卡", needBan));
        strings.add(new StringProjectEntity("任职安排", needBan));
        strings.add(new StringProjectEntity("薪酬标准", needBan));
        strings.add(new StringProjectEntity("头像/材料", needBan));
        mAdapter.setList(strings);
        mAdapter.updateItem(0);

        IdCardFragment idCardFragment = new IdCardFragment();
        ContactInformationFragment contactInformationFragment = new ContactInformationFragment();
        BankCardFragment bankCardFragment = new BankCardFragment();
        EmploymentInformationFragment employmentInformationFragment = new EmploymentInformationFragment();
        SalaryFragment salaryFragment = new SalaryFragment();
        OtherFragment otherFragment = new OtherFragment();

        idCardFragment.setArguments(getIntent().getExtras());
        contactInformationFragment.setArguments(getIntent().getExtras());
        bankCardFragment.setArguments(getIntent().getExtras());
        employmentInformationFragment.setArguments(getIntent().getExtras());
        salaryFragment.setArguments(getIntent().getExtras());
        otherFragment.setArguments(getIntent().getExtras());

        fragmentList.add(idCardFragment);
        fragmentList.add(contactInformationFragment);
        fragmentList.add(bankCardFragment);
        fragmentList.add(employmentInformationFragment);
        fragmentList.add(salaryFragment);
        fragmentList.add(otherFragment);

        mDatabind.viewpager2AddProject.setUserInputEnabled(false);
        mDatabind.viewpager2AddProject.setOffscreenPageLimit(fragmentList.size());
        mDatabind.viewpager2AddProject.setAdapter(new FragmentLazyStateAdapter(this, fragmentList));
    }

    @Override
    public void initObserver() {
        //关闭
        App.appViewModelInstance.getCreateFinish().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                AppExtKt.finishActivity(AddProjectActivity.class);
            }
        });
        //上一步，如果是编辑状态上一部需要把之前的内容都给灰色
        App.appViewModelInstance.getProjectLastPersonnel().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer position) {
                mAdapter.updateItem(position);
                mDatabind.viewpager2AddProject.setCurrentItem(position, false);
                if (type != 3) {//编辑的时候，不走这个逻辑 主要是为了灰色的那个效果
                    mAdapter.getData().get(position + 1).setNeedBan(true);
                } else {
                    mAdapter.getData().get(position).setNeedBan(false);
                }
            }
        });
        //跳转
        App.appViewModelInstance.getProjectPersonnel().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer position) {
                mAdapter.getData().get(position).setNeedBan(false);
                mAdapter.updateItem(position);
                mDatabind.viewpager2AddProject.setCurrentItem(position, false);
            }
        });
    }

    @Override
    public void onBindViewClick() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                //如果是新建的 就不让点击 0是新建 1、2是草稿 3是编辑
                if (getIntent() == null || getIntent().getExtras() == null) {
                    ToastUtil.show("添加时，请依次填写");
                    return;
                }
                if (type != 3) {
                    ToastUtil.show("添加时，请依次填写");
                    return;
                }
                mAdapter.getData().get(position).setNeedBan(false);
                mAdapter.updateItem(position);
                mDatabind.viewpager2AddProject.setCurrentItem(position, false);
            }
        });
    }


    @Override
    public void onRequestSuccess() {
        //用工规则
        mViewModel.getWorkRulesEntity().observe(this, new Observer<WorkRulesEntity>() {
            @Override
            public void onChanged(WorkRulesEntity workRulesEntity) {
                if ("1".equals(workRulesEntity.getIs_entry_sign())) {
                    MMKVHelper.putBoolean(ConstantMMVK.IS_ENTRY_SIGN, true);
                } else {
                    MMKVHelper.putBoolean(ConstantMMVK.IS_ENTRY_SIGN, false);
                }

                if ("1".equals(workRulesEntity.getIs_upload_not_guilty())) {
                    MMKVHelper.putBoolean(ConstantMMVK.IS_UPLOAD_NOT_GUILTY, true);
                } else {
                    MMKVHelper.putBoolean(ConstantMMVK.IS_UPLOAD_NOT_GUILTY, false);
                }

                if ("1".equals(workRulesEntity.getIs_entry_before_today())) {
                    MMKVHelper.putBoolean(ConstantMMVK.IS_ENTRY_BEFORE_TODAY, true);
                } else {
                    MMKVHelper.putBoolean(ConstantMMVK.IS_ENTRY_BEFORE_TODAY, false);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}